<template>
  <div class="patrolRoute">
    <div class="tableList">
      <div class="add">
        <span>巡视点列表</span>
        <el-button
          size="mini"
          type="primary"
          icon="el-icon-plus"
          v-if="!unEdit"
          @click="addPatrolPoint"
        >
          新增
        </el-button>
      </div>
      <div class="table">
        <base-table :tableSetting="patrolRouteConfig"></base-table>
      </div>
    </div>
    <dialogPublic
      title="关联巡视点"
      :show.sync="show"
      width="60%"
      @doSubmit="doSubmit"
      @doClose="() => (show = false)"
    >
      <relatePage
        v-if="show"
        :pointList="patrolRouteConfig.data"
        ref="relatePage"
      ></relatePage>
    </dialogPublic>
  </div>
</template>

<script>
import relatePage from "../patrolPoint/relatePage.vue";
export default {
  props: {
    pointList: {
      type: Array,
      default: () => {
        return [];
      },
    },
    unEdit: {
      type: Boolean,
      default: false,
    },
  },
  components: {
    relatePage,
  },
  data() {
    return {
      show: false,
      searchData: {},
      patrolRouteConfig: {
        indexShow: true,
        highlightRow: false,
        column: [
          {
            label: "巡视点",
            textAlign: "center",
            prop: "name",
          },
          {
            label: "操作",
            textAlign: "center",
            type: "button",
            width: "200",
            hide: false,
            buttonArr: [
              {
                text: "删除",
                isShow: () => {
                  return !this.unEdit;
                },
                methods: (row, index) => {
                  this.$confirm("确定要删除该条数据吗", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                  })
                    .then(() => {
                      this.patrolRouteConfig.data.splice(index, 1);
                    })
                    .catch((e) => {});
                },
              },
              {
                text: "上移",
                isShow: () => {
                  return !this.unEdit;
                },
                methods: (row, index) => {
                  this.moveUp(row, index);
                },
              },
              {
                text: "下移",
                isShow: () => {
                  return !this.unEdit;
                },
                methods: (row, index) => {
                  this.moveDown(row, index);
                },
              },
            ],
          },
        ],
        data: [],
        noBorderRight: false,
        autoHeight: true,
      },
    };
  },
  created() {
    if (this.unEdit) {
      this.patrolRouteConfig.column[1].hide = true;
    } else {
      this.patrolRouteConfig.column[1].hide = false;
    }
  },
  watch: {
    pointList: {
      handler(val) {
        this.patrolRouteConfig.data = val;
      },
    },
    unEdit: {
      handler(val) {
        if (val) {
          this.patrolRouteConfig.column[1].hide = true;
        } else {
          this.patrolRouteConfig.column[1].hide = false;
        }
      },
    },
  },
  methods: {
    //添加巡视点
    addPatrolPoint() {
      this.show = true;
    },
    //上移
    moveUp(row, index) {
      if (index > 0) {
        let upDate = this.patrolRouteConfig.data[index - 1];
        this.patrolRouteConfig.data.splice(index - 1, 1);
        this.patrolRouteConfig.data.splice(index, 0, upDate);
      } else {
        alert("已经是第一条，不可上移");
      }
    },
    //下移
    moveDown(row, index) {
      if (index + 1 === this.patrolRouteConfig.data.length) {
        alert("已经是最后一条，不可下移");
      } else {
        // 保存下一条数据
        let downDate = this.patrolRouteConfig.data[index + 1];
        // 删除下一条数据
        this.patrolRouteConfig.data.splice(index + 1, 1);
        // 增添被删除的那一条数据
        this.patrolRouteConfig.data.splice(index, 0, downDate);
      }
    },
    //确定选择巡视点
    doSubmit() {
      let data = this.$refs.relatePage.getSelectData();
      this.patrolRouteConfig.data = data;
      this.show = false;
    },
    //获取巡视点列表
    getPointList() {
      return this.patrolRouteConfig.data;
    },
  },
};
</script>

<style lang="less" scoped>
.patrolRoute {
  .tableList {
    height: 100%;
    margin-top: 20px;
    .add {
      margin: 10px 0;
      display: flex;
      span {
        margin: 0 25px 0 20px;
      }
      span::before {
        content: "*";
        color: red;
        margin-right: 4px;
      }
    }
    .table {
      width: 60%;
      margin-left: 120px;
    }
  }
}
</style>